<?php 
/**
 * 商品详细控制器
 */
class ContentController extends Controller{
    /**
     * 
     */
    public function index(){
    	$gid = Q('get.gid',0,'intval');
		//商品基本信息
		$goodsData = K('Goods')->where("gid={$gid}")->find();
		$this->assign('goodsData',$goodsData);
		
		//商品属性
		$attrData = M()->join('__goods_attr__ ga JOIN __attribute__ ta ON ga.attribute_atid=ta.atid')->where("goods_gid={$gid} AND at_type=1")->order('atid')->all();
		//***************
		$specTemp = array();
		foreach ($attrData as $v) {
			$specTemp[$v['atid']][] = $v;
		}
		$tempSpec = array();
		foreach ($specTemp as $taid => $value) {
			$tempSpec[] = array(
				'name' => $value[0]['atname'],
				'value' => $value
			);
		}
		$this->assign('tempSpec',$tempSpec);
		//****************

		//商品详细表信息
		$DetData = K('Detail')->where("goods_gid={$gid}")->find();
		//组合图册路径
		if($DetData['good_pic']){
			$DetData['good_pic'] = unserialize($DetData['good_pic']);
			foreach ($DetData['good_pic'] as $k => $v) {
				//获得类型
				$type = strrchr($v, '.');
				//小图
				$smallPath = str_replace($type, '_small' . $type, $v);
				//中图
				$midPath = str_replace($type, '_mid' . $type, $v);
				//大图
				$bigPath = str_replace($type, '_big' . $type, $v);	
				//上传图
				$thumbPath = str_replace($type, '_thumb' . $type, $v);		
				$DetData['good_pic'][$k] = array(
					'small' => $smallPath,
					'mid' => $midPath,
					'big' => $bigPath
				);
			}
			$this->assign('DetData',$DetData);
//			p($DetData);
		}
		
		
		//处理面包屑
		$cateData = K("Cate")->all();
		$cid = $goodsData['category_cid'];
		$fatherCate = $this->_getFater($cateData,$cid);
		//数组反转
		$fatherCate = array_reverse($fatherCate);
		$this->assign('fatherCate',$fatherCate);
		
		//*************************************************************
		//第一层楼衣服数据
		$cateAll = K('Cate')->all();
		//1.根据当前分类的cid,获得所有的子集cid
		$sonCids = $this->_getSonCid($cateAll,1);
		if(!$sonCids){
			$Clothes = array();
		}else{
			//2.根据cid获得所有的商品gid
			$Clothes = M()->join('__goods__ g JOIN __brand__ b ON b.bid=g.brand_bid')->where('category_cid in(' . implode(',', $sonCids) . ')')->all();
		}
		$this->assign('Clothes',$Clothes);
		
		
		
		
        $this->display(); 
    }

	/**
	 * 找到分类cid的父级
	 */
	private function _getFater($cateData,$cid){
		$temp = array();
		foreach ($cateData as $v) {
			if($v['cid'] == $cid){
				$temp[] = $v;
				//子集的pid即为父级的cid
				$temp = array_merge($temp,$this->_getFater($cateData, $v['pid']));
			}
		}
		return $temp;
	}	
	
	/**
	 * 异步获得商品价格信息
	 */
	public function getAdd(){
	    if(IS_AJAX){
	    	$combine = rtrim($_POST['id'],',');
			$gid = $_POST['gid'];
			//商品的信息
			$Goodinfo = K('GoodList')->where("goods_gid={$gid} AND combine like '{$combine}'")->find();
			//附加价格
			$combine = explode(',', $combine);
			$specArr = array();
			foreach ($combine as $v) {
				$specArr[] = K('Goods_Attr')->where("goods_gid={$gid} AND gtid={$v}")->getField('added');
			}
			$num = K('Goods')->where("gid={$gid}")->getField('shopprice');
			$specArr = array_sum($specArr);
			$Goodinfo['specArr'] = $num+$specArr;
			//最后价格
			echo json_encode($Goodinfo);
			exit;
	    }
	}


	/**
	 * 获得当前分类所对应所有的子集cid
	 */
	private function _getSonCid($cateData,$cid){
		$temp = array();
		foreach ($cateData as $cate) {
			if($cate['pid'] == $cid){
				$temp[] = $cate['cid'];
				$temp = array_merge($temp,$this->_getSonCid($cateData, $cate['cid']));
			}
		}
		return $temp;
	}















 }
 
 
 
 
 
 
 
 
 
 
 




 ?>